package com.fas.system.service.impl;

import com.fas.common.core.redis.RedisCache;
import com.fas.common.utils.DateUtils;
import com.fas.system.domain.FasInspectionLine;
import com.fas.system.domain.FasInspectionPlan;
import com.fas.system.mapper.FasInspectionLineMapper;
import com.fas.system.mapper.FasInspectionPlanIntervalMapper;
import com.fas.system.mapper.FasInspectionPlanMapper;
import com.fas.system.service.IFasInspectionPlanService;
import com.fas.system.service.IFasInspectionTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FasInspectionPlanServiceImpl implements IFasInspectionPlanService {

    private static final Logger log = LoggerFactory.getLogger(FasInspectionPlanServiceImpl.class);


    @Resource
    private FasInspectionPlanMapper fasInspectionPlanMapper;
    @Resource
    private FasInspectionLineMapper fasInspectionLineMapper;
    @Resource
    private FasInspectionPlanIntervalMapper fasInspectionPlanIntervalMapper;
    @Resource
    private RedisCache redisCache;
    @Resource
    private IFasInspectionTaskService fasInspectionTaskService;

    @Override
    public List<FasInspectionPlan> queryAll(FasInspectionPlan fasInspectionPlan) {
        List<FasInspectionPlan> list = fasInspectionPlanMapper.queryAll(fasInspectionPlan);
        list.forEach(ll -> ll.setCompanyName(FasCooperationCompanyServiceImpl.COMPANY_NAMES.get(ll.getCooperationCompanyId())));
        return list;
    }

    @Override
    public FasInspectionPlan findById(Integer id) {
        return fasInspectionPlanMapper.findById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertFasInspectionPlan(FasInspectionPlan fasInspectionPlan) {
        int count = fasInspectionPlanMapper.insertFasInspectionPlan(fasInspectionPlan);
        Integer planId = fasInspectionPlan.getId();

        //更新巡检路线表
        fasInspectionLineMapper.updatePlanByLineId(fasInspectionPlan.getLineId(),planId);

        //判断巡检时间段是否为空且巡检计划表中的数据添加成功
        List<String> intervalList = fasInspectionPlan.getIntervalList();

        if(intervalList!= null && intervalList.size()>0 && count > 0){
            fasInspectionPlanIntervalMapper.batchInsertInterval(intervalList,planId);

            //添加巡检任务表
            fasInspectionTaskService.insertFasInspectionTask(fasInspectionPlan);
//            if(fasInspectionPlan.getStartNow() != null && fasInspectionPlan.getStartNow() == 1){ //如果是立即生成任务则短信通知巡检组的所有巡检员
//                try {
//                    SendSmsResponse smsResponse = SendSmsUtil.sendSms("18328035013", getCode(fasInspectionPlan));
//                    log.debug("短信发送状态：" + smsResponse.getCode()+ "message : " + smsResponse.getMessage());
//                } catch (ClientException e) {
//                    e.printStackTrace();
//                }
//            }
        }



        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateFasInspectionPlan(FasInspectionPlan fasInspectionPlan) {
        int count = fasInspectionPlanMapper.updateFasInspectionPlan(fasInspectionPlan);

        //更新巡检路线表中的inspection_plan_id字段为空
        fasInspectionLineMapper.updatePlanByLineIds(new Integer[]{fasInspectionPlan.getId()});
        //更新巡检路线表
        fasInspectionLineMapper.updatePlanByLineId(fasInspectionPlan.getLineId(),fasInspectionPlan.getId());

        List<String> intervalList = fasInspectionPlan.getIntervalList();
        if(intervalList!= null && intervalList.size()>0 && count > 0){
            //先删除时间段表中的数据再添加
            Integer[] planId = {fasInspectionPlan.getId()};
            fasInspectionPlanIntervalMapper.deleteByFasInspectionPlanId(planId);
            fasInspectionPlanIntervalMapper.batchInsertInterval(intervalList,fasInspectionPlan.getId());

            // 通过巡检路线的id先删除巡检任务的数据，再添加数据
            Long[] lineId = {fasInspectionPlan.getLineId()};
            fasInspectionTaskService.deleteFasInspectionTask(lineId);
            fasInspectionTaskService.insertFasInspectionTask(fasInspectionPlan);
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteFasInspectionPlan(Integer[] ids,Long[] lineIds) {
        int count = fasInspectionPlanMapper.deleteFasInspectionPlan(ids);
        // 删除巡检计划下的巡检时间段
        fasInspectionPlanIntervalMapper.deleteByFasInspectionPlanId(ids);
        //更新巡检路线表中的inspection_plan_id字段为空
        fasInspectionLineMapper.updatePlanByLineIds(ids);
        // 删除巡检任务数据
        fasInspectionTaskService.deleteFasInspectionTask(lineIds);

        return count;
    }

    @Override
    public List<FasInspectionLine> selectNullLine(Long deptId,Integer cooperationCompanyId) {
        return fasInspectionLineMapper.selectNullLine(deptId,cooperationCompanyId);
    }

    private String getCode(FasInspectionPlan f){
        String startDate = DateUtils.dateTime(f.getStartDate());
        String endDate = DateUtils.dateTime(f.getEndDate());
//        return  String.format("尊敬的巡检员，您在 %s~%s 的 %s 时间段，将持续 %s 个周期的巡检任务",startDate,
//                endDate,StringUtils.join(f.getIntervalList(),","),f.getCycle());
        return  String.format("12345678901234567890");
    }
}
